#!/usr/bin/env python
"""
Acorn-SDR ('A Collection of Radio Nodes')
(c) Bob Cowdery (G3UKB) 2008

File: persist.py
Date: July 2008

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

================================================================================
Module Description:

The persistence layer.

"""

#-------------------------------------------------------------------------------
# System imports
import pickle
import string

# Application imports
import dynamic
import profiles
import capabilities

#-------------------------------------------------------------------------------
# Restored data sets
dynamic_new = None
profiles_new = None
capabilities_new = None
# The current profile and capability
profile = 'Dashboard'
capability = None

#-------------------------------------------------------------------------------
# Restore the data
# Dynamic    
try:
    f = open('acorn_dynamic.db', 'rb')
    dynamic_new = pickle.load(f)
    f.close()
    # Check for changes to the profile data
    for key in dynamic_new.keys():
        if dynamic.dynamic[key]['version'] > dynamic_new[key]['version']:
            # Version update        
            dynamic_new[key] = dynamic.dynamic[key]
except:
    # First time
    f = open('acorn_dynamic.db', 'wb')
    pickle.dump(dynamic.dynamic, f)
    f.close()
    f = open('acorn_dynamic.db', 'rb')
    dynamic_new = pickle.load(f)
    f.close()
# Profiles    
try:
    f = open('acorn_profiles.db', 'rb')
    profiles_new = pickle.load(f)
    f.close()
    # Check for changes to the profile data
    for key in profiles_new.keys():
        if profiles.profiles[key]['version'] > profiles_new[key]['version']:
            # Version update        
            profiles_new[key] = profiles.profiles[key]
except:
    # First time
    f = open('acorn_profiles.db', 'wb')
    pickle.dump(profiles.profiles, f)
    f.close()
    f = open('acorn_profiles.db', 'rb')
    profiles_new = pickle.load(f)
    f.close()
# Capabilities
try:
    f = open('acorn_capability.db', 'rb')
    capabilities_new = pickle.load(f)
    f.close()
    # Check for changes to the capability data
    for key in capabilities_new.keys():
        if capabilities.capabilities[key]['version'] > capabilities_new[key]['version']:
            # Version update        
            capabilities_new[key] = capabilities.capabilities[key]
except:
    # First time
    f = open('acorn_capability.db', 'wb')
    pickle.dump(capabilities.capabilities, f)
    f.close()
    f = open('acorn_capability.db', 'rb')
    capabilities_new = pickle.load(f)
    f.close()

#-------------------------------------------------------------------------------    
# Profiles API
def get_profiles():
    keys = profiles_new.keys()
    profile_str = ''
    first = True
    for key in keys:
        if not first:
            profile_str = profile_str + ','
        else:
            first = False
        profile_str = profile_str + profiles_new[key]['type'] + ':' + key
    return profile_str    

#-------------------------------------------------------------------------------    
def set_profile(the_profile):
    global profile
    global capability
    
    profile = the_profile
    capability = profiles_new[profile]['capability']

#-------------------------------------------------------------------------------    
def get_profile():
    global profile
    
    return profile

#-------------------------------------------------------------------------------
# General get/put API
def get(category, key, radio):
    if profile == None: return 'Empty'
    
    if category == 'Profile':
        if key in profiles_new[profile]:
            return profiles_new[profile][key]
        else:
            return 'Empty'
    elif category == 'Capability':
        if key in capabilities_new[capability]:
            return capabilities_new[capability][key]
        else:
            return 'Empty'
    elif category == 'Dynamic':
        if key in dynamic_new[profile][radio]:
            return dynamic_new[profile][radio][key]
        else:
            return 'Empty'
    else:
        return 'Empty'
    
#-------------------------------------------------------------------------------    
def put(category, key, value, radio):
    # Just overwrite if its there
    if profile == None: return
    if category == 'Profile':
        profiles_new[profile][key] = value
    elif category == 'Capability':
        capabilities_new[profile['capability']][key] = value
    elif category == 'Dynamic':
        #print 'Setting ', profile, radio, key
        dynamic_new[profile][radio][key] = value
        
#-------------------------------------------------------------------------------
# Persist all
def save():
    f = open('acorn_profiles.db', 'wb')
    pickle.dump(profiles_new, f)
    f.close()
    f = open('acorn_capability.db', 'wb')
    pickle.dump(capabilities_new, f)
    f.close()
    f = open('acorn_dynamic.db', 'wb')
    pickle.dump(dynamic_new, f)
    f.close()